#include "DictProducer.h"


DictProducer::DictProducer()
{
    Configuration &conf = Configuration::GetConfiguration();
    conf.ReadConfiguration("conf/myconf.conf");

    _EnFiles = conf._configuration["EnglishPaper"];
    _CnFiles = conf._configuration["ChinesePaper"];
}

DictProducer::DictProducer(const string &dir)
{
}

DictProducer::DictProducer(const string &dir, SplitTool *splitTool)
: _cuttor(splitTool)
{
    cout << "DictProducer(const string &, SplitTool*)\n";
}

//大写字母转小写字母
void toLowerCase(std::string &str) {
    std::transform(str.begin(), str.end(), str.begin(),
                   [](unsigned char c) { return std::tolower(c); });
}

void DictProducer::buildEnDict()
{
    // 建立英文词典
    for (auto & filename : _EnFiles)
    {
        SplitTool EnSplitTool;
        map<string, int> dict;

        vector<string> AllWords = EnSplitTool.cut(filename);

        for (auto &ele : AllWords)
        {
            dict[ele]++;
        }

        for (auto &pair : dict)
        {
            _EnDict.push_back(pair);
        }
    }
}

void DictProducer::buildCnDict()
{
    // 建立中文词典
    for (auto & filename : _CnFiles)
    {
        SplitToolCppJieba CnSplitTool;
        map<string, int> dict;

        vector<string> AllWords = CnSplitTool.cut(filename);

        for (auto &ele : AllWords)
        {
            dict[ele]++;
        }

        for (auto &pair : dict)
        {
            _CnDict.push_back(pair);
        }
    }
}

void DictProducer::createIndex()
{
    // 写入英文索引
    for (size_t idx = 0; idx < _EnDict.size(); ++idx)
    {
        for (char c : _EnDict[idx].first)
        {
            string key(1, c);
            _index[key].insert(idx);
        }
    }

    // 写入中文索引
    for (size_t idx = 0; idx < _CnDict.size(); ++idx)
    {
        for (size_t i = 0; i < _CnDict[idx].first.size();)
        {
            if ((_CnDict[idx].first[i] & 0x80) == 0)
            {
                string key(1, _CnDict[idx].first[i]);
                toLowerCase(key);
                _index[key].insert(idx);
                ++i;
            }
            else if ((_CnDict[idx].first[i] & 0xE0) == 0xC0)
            {
                string key = _CnDict[idx].first.substr(i, 2);
                _index[key].insert(idx);
                i += 2;
            }
            else if ((_CnDict[idx].first[i] & 0xF0) == 0xE0)
            {
                string key = _CnDict[idx].first.substr(i, 3);
                _index[key].insert(idx);
                i += 3;
            }
            else if ((_CnDict[idx].first[i] & 0xF8) == 0xF0)
            {
                string key = _CnDict[idx].first.substr(i, 4);
                _index[key].insert(idx);
                i += 4;
            }
            else
            {
                ++i;
            }
        }
    }
}

void DictProducer::store()
{
    // 写入英文词典
    ofstream EnDictFile;
    EnDictFile.open("data/EnDict.dat");
    for (auto &ele : _EnDict)
    {
        EnDictFile << ele.first << " " << ele.second << "\n";
    }

    // 写入中文词典
    ofstream CnDictFile;
    CnDictFile.open("data/CnDict.dat");
    for (auto &ele : _CnDict)
    {
        CnDictFile << ele.first << " " << ele.second << "\n";
    }

    // 写入索引
    ofstream DictIndex;
    DictIndex.open("data/DictIndex.dat");
    for (auto &ele : _index)
    {
        DictIndex << ele.first << " ";
        for (auto elem : ele.second)
        {
            DictIndex << elem << " ";
        }
        DictIndex << "\n";
    }
}
